/**
 * 循环滚动的游戏背景
 */

class BackGround extends Laya.Sprite {
    //定义背景
    private bg1 : Laya.Sprite;
    private bg2 : Laya.Sprite;
    //鱼钩是否处于上升状态
    public isMoveUp : boolean;
    //鱼钩当前深度(像素)
    public depth : number;
    //本次最大深度(像素)
    private maxDepth : number;
    //上次创建鱼后，背景已经上下移动了多少像素
	public curOffset : number;
    //是否处于加速滚动状态
    private isQuick : boolean;

    constructor() {
        super();

        //创建背景
        this.bg1 = new Laya.Sprite();
        this.bg1.loadImage("ui/bg.png");
        this.addChild(this.bg1);
        this.bg2 = new Laya.Sprite();
        this.bg2.loadImage("ui/bg.png");
        this.bg2.pos(0, Laya.stage.height);
        this.addChild(this.bg2);
    }
    
    Init(lineLen:number) : void {
        this.bg1.pos(0, 0);
        this.bg2.pos(0, Laya.stage.height);
        this.isMoveUp = false;
        this.depth = 0;
        this.maxDepth = lineLen * ComCfg.MeterPixel;
        this.curOffset = 0;
        this.isQuick = false;
    }

    OnLoop() : void {
        if(!this.isMoveUp){
            //鱼钩下降
            //背景容器每帧向上移动一定像素
            this.y -= ComCfg.DownPixel;
            this.depth += ComCfg.DownPixel;
            this.curOffset += ComCfg.DownPixel;
            //如果背景图到了不可见的位置，立马调整到下边循环显示
            if(this.bg1.y + this.y <= -800){
                this.bg1.y += 800*2;
            }
            if(this.bg2.y + this.y <= -800){
                this.bg2.y += 800*2;
            }

            if(this.depth >= this.maxDepth){
                this.isMoveUp = true;
                this.curOffset = 0;
            }
        }
        else{
            //鱼钩上升
            let curSpeed = ComCfg.UpPixel;
            if(this.isQuick){
                curSpeed = ComCfg.UpPixelQuick;
            }
            this.y += curSpeed;
            this.depth -= curSpeed;
            this.curOffset += curSpeed;
            if(this.bg1.y + this.y >= 800){
                this.bg1.y -= 800*2;
            }
            if(this.bg2.y + this.y >= 800){
                this.bg2.y -= 800*2;
            }
            
            if(this.depth <= 0){
                //本次钓鱼结束
                this.End();
                GameMain.viewGame.End();
            }
        }
    }

    End() : void {
        //停止滚动
        Laya.timer.clear(this, this.OnLoop);
    }

    //加速滚动
    Quick() : void {
        this.isQuick = true;
    }
}